// pages/add-purchase-goods-order/index.js
import { toPrevPage } from "../../utils/util";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabActive: "0",
    show: false,
    name: "",
    number: "",
    mome: "",
    id: "",
    type: "",
    nameIsError: "",
    numberIsError: "",
    list: [],
    images: [],
    fileList: [],
  },

  handleSure() {
    const { name, number, mome, list } = this.data;

    if (!name) {
      this.setData({
        nameIsError: "请输入产品名称",
      });

      return;
    }

    if (!number) {
      this.setData({
        numberIsError: "请输入购买数量",
      });

      return;
    }

    list.push({
      name,
      number,
      mome,
    });

    this.setData({
      list,
      show: false,
      name: "",
      number: "",
      mome: "",
      nameIsError: "",
      numberIsError: "",
    });
  },

  handleTabClick(event) {
    const { value } = event.currentTarget.dataset;

    this.setData({
      tabActive: value,
    });
  },

  handleDelete1(event) {
    const { index } = event.currentTarget.dataset;

    wx.showModal({
      title: "提示",
      content: "确定删除？",
      success: (res) => {
        if (res.confirm) {
          const { list } = this.data;

          list.splice(index, 1);

          this.setData({
            list,
          });
        } else {
          console.log("用户点击取消");
        }
      },
    });
  },

  handleAddItem() {
    this.setData({
      show: true,
    });
  },

  handleClose() {
    this.setData({
      show: false,
    });
  },

  handleSubmit() {
    const { tabActive, id, type } = this.data;

    if (tabActive === "1") {
      // 内容
      const { list } = this.data;

      if (list && list.length === 0) {
        wx.showToast({
          title: "请添加进货明细",
          icon: "none",
          duration: 2000,
        });

        return;
      } else {
        const arr = list.map((item) => {
          return { product_name: item.name, product_count: item.number };
        });

        // console.log(arr);
        let order_type = "";

        if (type == "5") {
          order_type = 1;
        } else if (type == "4") {
          order_type = 2;
        } else if (type == "3") {
          order_type = 3;
        }

        const param = {
          order_type: order_type,
          merchant_id: id,
          order_detail: JSON.stringify(arr),
        };

        wx.ajax.post("addPurchaseOrder.action", param, true).then((res) => {
          // console.log(res);
          if (res.success) {
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 2000,
            });

            const tiemr = setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              });
              clearTimeout(tiemr);
            }, 1000);
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 2000,
            });
          }
        });
      }
    } else if (tabActive === "0") {
      // 图片
      const { fileList, images } = this.data;

      // console.log(fileList);
      if (fileList && fileList.length === 0) {
        wx.showToast({
          title: "请添加图片",
          icon: "none",
          duration: 2000,
        });

        return;
      }

      let order_type = "";

      if (type == "5") {
        order_type = 1;
      } else if (type == "4") {
        order_type = 2;
      } else if (type == "3") {
        order_type = 3;
      }

      const params = {
        order_type: order_type,
        merchant_id: id,
        order_img: images.join(","),
      };

      wx.ajax.post("addPurchaseOrder.action", params, true).then((res) => {
        // console.log(res);
        if (res.success) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000,
          });

          const tiemr1 = setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            });

            clearTimeout(tiemr1);
          }, 1000);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000,
          });
        }
      });
    }
  },

  deleteHandle(event) {
    const { index } = event.detail;

    const { fileList, images } = this.data;
    fileList.splice(index, 1);
    images.splice(index, 1);
    this.setData({
      fileList,
      images,
    });
  },
  afterReadHandle(event) {
    const file = event.detail.file;

    const { fileList, images } = this.data;

    wx.showLoading({
      title: "上传中...",
    });
    wx.ajax.uploadFile({
      filePath: file.url,
      callback: (url) => {
        wx.hideLoading();
        if (url) {
          fileList.push(file);
          images.push(url);
          this.setData({
            fileList,
            images,
          });
        }
      },
    });
  },

  handleNameInput() {
    const { name } = this.data;

    this.setData({
      nameIsError: name ? "" : "请输入产品名称",
    });
  },

  handleNumberInput() {
    const { number } = this.data;

    this.setData({
      numberIsError: number ? "" : "请输入购买数量",
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
      });
    }

    if (options.type) {
      this.setData({
        type: options.type,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
